module CustomMatchers

  class BeenCreatedWithAttributes
  
    def initialize(expected)
      @expected = expected
    end
  
    def matches?(target)
      @target = target.reload
      
      @expected.each_key do |attr|
        return false unless @target[attr] == @expected[attr]
      end
  
      true
    end
  
    def failure_message
      "expected attribs:\n\n #{@expected.inspect}\n\nbut got\n\n#{target_attributes.inspect}"
    end

    def negative_failure_message
      "did not expect attribs:\n\n #{@expected.inspect} to all be present"
    end

    private

    def target_attributes
      the_target_attributes = {}

      @expected.each_key do |attr|
        the_target_attributes[attr] = @target.attributes[attr.to_s]
      end

      the_target_attributes 
    end

  end # of class

  def been_created_with_attributes(expected)
    BeenCreatedWithAttributes.new(expected)
  end

end # of module
